package com.xiaobawang.hundouluo.fragment;


import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.os.SystemClock;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Chronometer;
import android.widget.CompoundButton;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import com.xiaobawang.hundouluo.R;
import com.xiaobawang.hundouluo.bean.Equipment;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class EquipmentFragment extends Fragment {


    private List<Equipment> list;  //数据源
    private BaseAdapter adapter;  //适配器
    private GridView gv;  //控件

    public EquipmentFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        View root =  inflater.inflate(R.layout.fragment_equipment, container, false);

        //找出GridView、定时器组件
        gv = root.findViewById(R.id.fragment_equipment_gv);

        //new出数据源
        list = new ArrayList<>();
        list.add(new Equipment("报警器", "", R.mipmap.baojingqi));
        list.add(new Equipment("通风机", "", R.mipmap.tongfengji));
        list.add(new Equipment("照明灯", "", R.mipmap.zhaomingdeng));
        list.add(new Equipment("抽水泵", "", R.mipmap.choushuibeng));

        //new适配器
        adapter = new BaseAdapter() {
            @Override
            public int getCount() {
                return list.size();
            }

            @Override
            public Object getItem(int i) {
                return list.get(i);
            }

            @Override
            public long getItemId(int i) {
                return i;
            }

            @Override
            public View getView(int i, View view, ViewGroup viewGroup) {

                //找出gv_equipment_item里的布局文件
               final View v = LayoutInflater.from(EquipmentFragment.this.getContext()).inflate(R.layout.gv_equipment_item, null);

                //找控件
                TextView equipmentName = v.findViewById(R.id.gv_equipment_item_tv_equipmentName);
//                TextView equipmentDuration = v.findViewById(R.id.gv_equipment_item_tv_equipmentDuration);
                final Chronometer timer = v.findViewById(R.id.gv_equipment_item_tv_equipmentDuration);
                ImageView equipmentPicture = v.findViewById(R.id.gv_equipment_item_iv_equipmentPicture);
                final TextView equipmentStatus = v.findViewById(R.id.gv_equipment_item_tv_equipmentStatus);
                Switch equipmentSwitch = v.findViewById(R.id.gv_equipment_item_switch);

                //Switch绑定事件
                equipmentSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                        String status;
                        if(b) {
                            status = "开启";
                            timer.setBase(SystemClock.elapsedRealtime());//计时器清零
                            int hour = (int) ((SystemClock.elapsedRealtime() - timer.getBase()) / 1000 / 60);
                            timer.setFormat("0"+String.valueOf(hour)+":%s");
                            timer.start();
                        } else {
                            status = "关闭";
                            timer.stop();
                        }
                        equipmentStatus.setText(status);
                    }
                });

                //数据绑定
                equipmentName.setText(list.get(i).getEquipmentName());
                equipmentPicture.setImageResource(list.get(i).getEquipmentPicture());
                return v;

            }
        };

        //GridView加载适配器
        gv.setAdapter(adapter);

        return root;


    }

}
